
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>文字2</title>
</head>
<body >
<canvas id="canvas" style="display: block;margin: 0 auto;border: 1px solid #aaa">
    您的浏览器不支持canvas
</canvas>
<script>
    window.onload=function(){
        var canvas=document.getElementById("canvas");
        canvas.width=800;
        canvas.height=800;
        var context=canvas.getContext("2d");


        context.fillStyle="#058";
        context.font="bold 40px sans-serif";


        context.textBaseline="top";
        context.fillText("人生若只如初见，何事秋风悲画扇",40,100);
        var testWidth=context.measureText("人生若只如初见，何事秋风悲画扇").width;
        context.fillText("以上字符串的宽度为"+testWidth+"px",40,200)

    }
</script>
</body>
</html>